# send_email.py

import argparse
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from config import Config

def send_email(subject, body, recipient):
    # 使用 Config 中的 SMTP 配置
    SMTP_SERVER = Config.SMTP_SERVER
    SMTP_PORT = Config.SMTP_PORT
    SMTP_USERNAME = Config.SMTP_USERNAME
    SMTP_PASSWORD = Config.SMTP_PASSWORD
    FROM_EMAIL = Config.FROM_EMAIL

    msg = MIMEText(body, 'plain', 'utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = FROM_EMAIL
    msg['To'] = recipient

    try:
        server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        server.starttls()
        server.login(SMTP_USERNAME, SMTP_PASSWORD)
        server.sendmail(FROM_EMAIL, [recipient], msg.as_string())
        server.quit()
        print(f'邮件已发送给 {recipient}')
    except Exception as e:
        print(f'发送邮件失败: {e}')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='发送邮件通知')
    parser.add_argument('--subject', required=True, help='邮件主题')
    parser.add_argument('--body', required=True, help='邮件内容')
    parser.add_argument('--recipient', required=True, help='收件人邮箱')

    args = parser.parse_args()
    send_email(args.subject, args.body, args.recipient)
